package DAO;

import java.util.List;

import interfacedao.EmprestimoDAO;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import modelo.Emprestimo;

/**
 * Session Bean implementation class EmprestimoDAOBean
 */
@Stateless
@LocalBean
public class EmprestimoDAOBean implements EmprestimoDAO {

	@PersistenceContext(unitName="prova")
	private EntityManager em;
	
    public EmprestimoDAOBean() {}
        
  

	@Override
	public List<Emprestimo> buscaPelaMatricula(String matricula) {
		return em.createNamedQuery("buscaEmprestimoPorMatricula",Emprestimo.class)
				.setParameter("matricula", matricula).getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Emprestimo> buscalivros() {
		Query query = em.createNamedQuery("listaEmprestimos", Emprestimo.class);
		return query.getResultList();
	}

	@Override
	public void criarEmprestimo(Emprestimo e) {
		em.persist(e);
		
	}

	@Override
	public Boolean apagarEmprestimo(Emprestimo e) {
		em.remove(e);
		return true;
	}


	@Override
	public void atualizaEmprestimo(Emprestimo e) {
		em.merge(e);
		
	}

}
